<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title><? echo $title ?></title>
<? require("lib.php"); ?>
    <style>
        .tag_choosed{ margin-left: 5px; margin-right: 5px}
        .tag_choose{margin-left: 5px; margin-right: 5px}
    </style>
</head>
<body>
<div class="content-box"><!-- Start Content Box -->
  <div class="content-box-header">
      <h3><? echo $title ?></h3>
      <div class="clear"></div>
  </div> <!-- End .content-box-header -->
  <div class="content-box-content">
<?
  switch ($do) {
    case "add":
    	add();
    	break;
  	case "update":
  		update($info);
  		break;
  	case "delete":
  		delete($id);
  		break;
      default:
  		show_info($info);
  }
 ?>
  <? function show_box($row, $iseditmode, $isadd=0){
			global $id, $h, $url, $title, $top_cate, $cate, $cateid, $page, $baseurl, $option, $arrlang, $lang, $arr_cate, $has_pic, $pic_height, $pic_width, $tag, $tag_list_info, $str_tag;
      echo $h->hidden('cid', $cateid);
      echo $h->hidden('page', $page);
		?>
    	<table width="99%" cellspacing="0" cellpadding="3" class="mbox">
          <tr class="th"><td class="thtitle" width="120" align="right"><? echo $title ?></td>
          <td><? echo $h->hidden('id', iso($row["id"])); ?></td></tr>
            <?
              $str='';
              //foreach($arrlang as $val){ // 加载所有启用的分语言的字段, add all fields that marked by languages
                $str.='<tr><td class="l">'.ROOT_NAME.'</td><td>'.$h->text('name', $row['name'], 'g').'</td></tr>';
                $str.='<tr><td class="l">'.ROOT_CONTENT.'</td><td>'.$h->textarea('content', $row['content'], 32, 16, 'editor g', '').'</td></tr>';
              //}
              echo $str;
            ?>
          <tr><td class="l"><?=ROOT_CATEGORY?></td><td><? echo $h->select('category_id', $row["category_id"], $arr_cate, 'id', 'name_'.$lang, 'g') ?></td></tr>
          <tr><td class="l"><?=ROOT_LANGUAGE?></td><td><? echo $h->select('language_id', $row["language_id"], $arrlang, 'id', 'name', 'g') ?></td></tr>
          <?
            // 附加字段, 按大类设置读取
            $str='';
            for($i=1;$i<4;$i++){
              if($top_cate['has_field'.$i]) $str.='<tr><td class="l">'.$top_cate['field'.$i.'_'.$lang].'</td><td>'.$h->text('field'.$i, $row['field'.$i], 'g').'</td></tr>';
            }
            echo $str;
          ?>
          <? $hide=''; if(! $has_pic) $hide=' class="hide" '; ?>
          <tr <? echo $hide ?>><td class="l"><?=ROOT_PICTURE?></td><td>
          <? if(!$isadd)echo '<img src="'.$baseurl.'upload/'.decode($row["pic"]).'" height="72" /><br />'; ?>
          <? echo $h->cfile('cpic', 'g') ?> <span class="red"> <?=ROOT_PICTURE_SIZE_RATIO?> <? echo $pic_width.':'.$pic_height ?>.</span>
          </td></tr>
          <tr><td class="l"><?=ROOT_KEYWORD?></td><td><? echo $h->text('keyword', ($row["keyword"]), 'g') ?></td></tr>
          <tr><td class="l"><?=ROOT_DESCRIPTION?></td><td><? echo $h->textarea('description', ($row["description"]),24,3, 'g') ?></td></tr>
          <tr><td class="l"><?=ROOT_ORDER?></td><td><? echo $h->text('seq_no', iso($row["seq_no"]), 'gs') ?> <span class="red">  <?=ROOT_HIGHER_NUMBER_IN_FRONT?></span></td></tr>
            <tr><td class="l"><?=ROOT_TAGS_CHOOSE?></td><td class="add_tag_child">&nbsp;
                    <input type="hidden" name="tag_list_str" value="<?=$str_tag?>"/>
                    <?
                    $str_tag = '';
                    $tag_id_arr = array();
                    if($tag){
                        foreach($tag as $key =>$val){
                            $str_tag .= '<a class="tag_choosed" href="javascript:void(0)" onclick="remove_tag(this)"><input type="hidden" name="tag_id" value="'.$val['tag_id'].'"/>'.$val['tag_name'].'</a>';
                            $tag_id_arr[] = $val['tag_id'];
                        }
                    }
                    $str_tag .= '&nbsp;<input type="text" class="gs tags_add_value" value="" name="tags_add">&nbsp;<a class="tags_add" onclick="tags_add_value()">'.ROOT_TAGS_ADD.'</a> &nbsp;<a class="tag_show" href="javascript:void(0)">'.ROOT_TAGS_COMMON_USE.'</a>';
                    echo $str_tag;
                    ?>
                </td></tr>
            <tr class="tag_list_show hide"><td></td><td>
                    <?foreach($tag_list_info as $key => $val){
                        $onclick = in_array($val['id'], $tag_id_arr) ? '' : 'onclick="add_tag(this)"';
                        echo '<a class="tag_choose" href="javascript:void(0)" '.$onclick.'><input type="hidden" name="tag_id" value="'.$val['id'].'">'.$val['name_'.$lang].'</a>';
                    }?>
                </td></tr>
            <tr><td class="l">&nbsp;</td><td><br><? echo $h->submit('save', '&nbsp; '.ROOT_SAVE.' &nbsp;', $iseditmode==1?'':'hide') ?>&nbsp;&nbsp;<a onClick="history.go(-1)">[ <?=ROOT_CANCEL?> ]</a><br><br></td></tr>
        </table>
        <? } ?>
			<? function show_boxes($result){
				global $url, $page, $pages, $cateid, $cate, $s, $h, $filter, $lang, $arrlang, $arr_cate, $gee_tags, $gee_tag_list, $tag, $tag_list_info, $str_tag;
                echo $h->form($url.'.php', 0, 'myform'); // 构建删除多项表单 form for deleting muti rows
                echo $h->hidden('action', ''); //
                echo $h->hidden('page', $page); // 当前页面 current page
                echo $h->hidden('s', $s); // 当前页面 current page
                echo $h->hidden('filter', $filter); // 当前页面 current page
        $str='<table width="99%" cellspacing="0" class="mlist"><tr class="th"><td><input type="checkbox" name="aller" class="check-all" /> '.ROOT_SELECT_ALL.'</td><td><strong>#</strong></td><td><strong>'.ROOT_NAME.'</strong></td><td><strong>'.ROOT_TAGS_CHOOSE.'</strong></td><td><strong>'.ROOT_CATEGORY.'</strong></td><td><strong>'.ROOT_ORDER.'</strong></td><td><a href="'.$url.'.php?cid='.$cateid.'&do=add">[ '.ROOT_ADD.' ]</a></td></tr>';
				$count=1;
        foreach( $result as $row)
				{
					$id=decode($row['id']);
          $temcate=$arr_cate[$row['category_id']]['name_'.$lang];
                    $where = array();
                    $where[]=array('name'=>'p_id','val'=>$id);
                    $where[]=array('name'=>'table_name','val'=>'news');
                    $tag = $gee_tag_list->get_rows_advanced(1024, $where);
                    $arr_tag = array();
                    for($i = 0; $i < count($tag); $i++){
                        $tag_info = $gee_tags->get_row($tag[$i]['tag_id']);
                        $tag[$i]['tag_name'] = $tag_info['name_'.$lang];
                        $arr_tag[] = $tag[$i]['tag_name'];
                    }
                    $tag_name = empty($arr_tag) ? '' : implode('<br>', $arr_tag);
          //$str.="<tr><td>".$id.".</td><td><a href=\"$url.php?do=update&id=".$id."\">".decode($row['name'])."</a></td><td>".$temcate."</td><td>".$row['seq_no']."</td><td><a href=\"$url.php?do=update&id=".$id."\">".ROOT_UPDATE."</a> | <a href=\"$url.php?do=delete&id=".$id."\" onClick=\"javascript: return delc();\">".ROOT_DELETE."</a></td></tr>";
          $str.='<tr><td><input class="aller" type="checkbox" name="all[]" value="'.$id.'" /></td><td>'.$id.'.</td><td><a href="'.$url.'.php?do=update&cid='.$cateid.'&page='.$page.'&id='.$id.'">'.decode($row['name']).'</a></td><td>'.$tag_name.'</td><td>'.$temcate.'</td><td>'.$row['seq_no'].'</td><td><a href="'.$url.'.php?do=update&cid='.$cateid.'&page='.$page.'&id='.$id.'">'.ROOT_UPDATE.'</a> | <a href="'.$url.'.php?do=delete&cid='.$cateid.'&pagee='.$page.'&id='.$id.'" onClick="javascript: return delc();">'.ROOT_DELETE.'</a></td></tr>';
					$count++;
				}
				$str.= "</table>";
				echo $str;
				if($count==1) echo "<br>".ROOT_NO_DATA."!";
                $h->former();
                ?>
                <table width="99%" cellspacing="0" class="mlist">
                    <tr><td colspan="2"><input class="button" onclick="check_all('deletes');return operator();" type="submit" value="<?=ROOT_DELETE_SELECTED?>" />&nbsp;<a class="button tag_show" href="javascript:void(0)"><?=ROOT_TAGS_ADD?></a></td></tr>
                    <tr class="hide tags_list_show"><td width="100"><?=ROOT_TAGS_CHOOSE?></td><td class="add_tag_child">&nbsp;<input type="hidden" name="tag_list_str" value=""/>
                            &nbsp;<input type="text" class="gs tags_add_value" value="" name="tags_add">&nbsp;<a class="tags_add" onclick="tags_add_value()"><?=ROOT_TAGS_ADD?></a>&nbsp;<input class="button" onclick="check_all('tags_add');return operator();" type="submit" value="<?=ROOT_ALL_ADD?>"/>
                        </td></tr>
                    <tr class="hide tag_list_show tags_list_show"><td width="100"><?=ROOT_TAGS_COMMON_USE?></td><td class="tag_list_show_i">
                            <?foreach($tag_list_info as $key => $val){
                                $onclick = 'onclick="add_tag(this)"';
                                echo '<a class="tag_choose" href="javascript:void(0)" '.$onclick.'><input type="hidden" name="tag_id" value="'.$val['id'].'">'.$val['name_'.$lang].'</a>';
                            }?>
                        </td></tr>
                </table>
                <?
				echo echo_page($page, $pages, $url.'-'.$cateid.'-p');
             } ?>
      <script type="text/javascript">
          /* $(function(){
           $('.del_all').click(function(){
           $('.myform input:hidden[name="action"]').val('deletes');
           $('.myform').submit();
           });
           });*/
          $(function(){
              var k = 1;
              $('.tag_show').click(function(){
                  if(k){
                      $('.tag_list_show').removeClass('hide');
                      $('.tags_list_show').removeClass('hide');
                      k = 0;
                  }else{
                      $('.tag_list_show').addClass('hide');
                      $('.tags_list_show').addClass('hide');
                      k = 1;
                  }
              });
          });

          function tags_add_value(){
              var tags_value = $('.tags_add_value').val();
              $.ajax({
                  'url':'tag_product_add.php?do=tag_add',
                  'data':{'value':tags_value},
                  'type':'post',
                  success:function(data){
                      var data_arr = eval("("+data+")");
                      if(data_arr.id){
                          $('.tags_add_value').before('<a class="tag_choosed" href="javascript:void(0)" onclick="remove_tag(this)"><input type="hidden" name="tag_id" value="'+data_arr.id+'"/>'+data_arr.value+'</a>');
                          $('.tag_choose').first().before('<a class="tag_choose" href="javascript:void(0)"><input type="hidden" name="tag_id" value="'+data_arr.id+'"/>'+data_arr.value+'</a>');
                          var tag_list_str = $('input:hidden[name="tag_list_str"]').val();
                          tag_list_str += ','+data_arr.id;
                          if(tag_list_str[0] == ','){
                              tag_list_str = tag_list_str.substring(1, tag_list_str.length);
                          }
                          $('input:hidden[name="tag_list_str"]').val(tag_list_str);
                      }
                  }
              });
          }

          function add_tag(node){
              $('.tags_add_value').before('<a class="tag_choosed" href="javascript:void(0)" onclick="remove_tag(this)">'+$(node).html()+'</a>');
              var tag_id = $(node).children('input:hidden').val();
              var tag_list_str = $('input:hidden[name="tag_list_str"]').val();
              tag_list_str += ','+tag_id;
              if(tag_list_str[0] == ','){
                  tag_list_str = tag_list_str.substring(1, tag_list_str.length);
              }
              $('input:hidden[name="tag_list_str"]').val(tag_list_str);
              $(node).removeAttr('onclick');
          }
          function remove_tag(node){
              $(node).remove();
              var tag_id = $(node).children('input:hidden').val();
              var tag_list_str = $('input:hidden[name="tag_list_str"]').val();
              var tag_arr = tag_list_str.split(',');
              tag_arr.splice($.inArray(tag_id, tag_arr), 1);
              tag_list_str= tag_arr.join(',');
              $('input:hidden[name="tag_list_str"]').val(tag_list_str);
              $('.tag_list_show a').each(function(){
                  if(!($(this).attr('onclick')) &&　($(this).children('input:hidden').val() == tag_id)){
                      $(this).attr('onclick', 'add_tag(this)');
                  }
              });
          }
          function check_all(otype){
              $('.myform input:hidden[name="action"]').val(otype);
              $('.myform').submit();
          }
      </script>
         <br class="clear" />
				</div> <!-- End .content-box-content -->
        <br class="clear" />
			</div><!-- End .content-box -->
</body>
</html>